//
//  HomeNavBar.swift
//  LeTu
//
//  Created by g1 on 2024/1/9.
//

import UIKit

class HomeNavBar: UIView, UITextFieldDelegate {
    
    var addressHandle: (()->Void)?
    var languageHandle: (()->Void)?
    var textFieldWillBeiginHandle: (()->Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        DispatchQueue.main.async {
            self.addressBtn.imagePosition(style: .right, spacing: 1)
            self.languageBtn.imagePosition(style: .right, spacing: 1)
        }
    }
    func setAddress(_ address: String) {
        addressBtn.setTitle(address.localized)
        
        // 更新布局
        setNeedsLayout()
        layoutIfNeeded()
    }
    func updateInfo(keyword: String) {
        languageBtn.setTitle(LanguageManager.shared.currentLanguage == .en ? "英文".localized:"中文".localized)
        addressBtn.setTitle(keyword.localized)
        searchTextField.placeholder = "搜索景点/目的地".localized+"                                                   "
        
        //        addressBtn.snp.reConstraints { make in
        //            make.left.equalTo(12)
        //            make.top.bottom.equalToSuperview()
        //        }
        
        // 更新布局
        setNeedsLayout()
        layoutIfNeeded()
    }
    
    // MARK: - UIButtonAction
    @objc func addressBtnAction() {
        guard let handle = addressHandle else { return }
        handle()
    }
    @objc func languageBtnAction() {
        guard let handle = languageHandle else { return }
        handle()
    }
    
    // MARK: - UITextFieldDelegate
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if let handle = textFieldWillBeiginHandle {
            handle()
        }
        return false
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    // MARK: - configUI
    func configUI() {
        
        addSubview(backView)
        backView.addSubview(addressBtn)
        backView.addSubview(languageBtn)
        backView.addSubview(searchBackView)
        searchBackView.addSubview(searchTextField)
        searchBackView.addSubview(searchBtn)
        
        searchBackView.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        searchBackView.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)

        backView.snp.makeConstraints { make in
            make.left.bottom.right.equalToSuperview()
            make.top.equalTo(Size.statusBarHeight)
        }
        addressBtn.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.top.bottom.equalToSuperview()
        }
        languageBtn.snp.makeConstraints { make in
            make.right.equalTo(-12)
            make.top.bottom.equalToSuperview()
        }
        searchBackView.snp.makeConstraints { make in
            make.left.equalTo(addressBtn.snp.right).offset(8)
            make.right.equalTo(languageBtn.snp.left).offset(-10)
            make.height.equalTo(38)
            make.centerY.equalToSuperview()
        }
        searchBtn.snp.makeConstraints { make in
            make.right.equalTo(-6)
            make.top.equalTo(6)
            make.width.equalTo(searchBtn.snp.height).multipliedBy(43.0/26.0)
            make.bottom.equalTo(-6)
        }
        searchTextField.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.lessThanOrEqualTo(searchBtn.snp.left).offset(-12)
            make.top.bottom.equalToSuperview()
        }
        
        addressBtn.setTitle("北京".localized)
        addressBtn.addTarget(self, action: #selector(addressBtnAction))
        languageBtn.addTarget(self, action: #selector(languageBtnAction))
        searchTextField.delegate = self

    }
    private lazy var backView: UIView = {
        let backView = UIView()
        return backView
    }()
    private lazy var addressBtn: CustomClickableView = {
        let addressBtn = CustomClickableView()
        addressBtn.setFont(kSetPingFangMedium(14))
        addressBtn.setTitleColor(.app_black)
        addressBtn.setImage(UIImage(named: "home_down_arrow"))
        return addressBtn
    }()
    private lazy var languageBtn: CustomClickableView = {
        let languageBtn = CustomClickableView()
        languageBtn.setFont(kSetPingFangMedium(14))
        languageBtn.setTitleColor(.app_black)
        languageBtn.setImage(UIImage(named: "home_down_arrow"))
        return languageBtn
    }()
    private lazy var searchBackView: UIView = {
        let searchBackView = UIView()
        searchBackView.backgroundColor = UIColor(red: 0.965, green: 0.965, blue: 0.965, alpha: 1)
        searchBackView.layer.cornerRadius = 19
        searchBackView.layer.masksToBounds = true
        return searchBackView
    }()
    private lazy var searchTextField: UITextField = {
        let searchTextField = UITextField()
        searchTextField.font = kSetPingFangRegular(14)
        searchTextField.returnKeyType = .search
        return searchTextField
    }()
    private lazy var searchBtn: CustomClickableView = {
        let searchBtn = CustomClickableView()
        searchBtn.setBackgroundImage(UIImage(named: "home_search_btn_background"))
        searchBtn.setImage(UIImage(named: "home_search_icon"))
        return searchBtn
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
